import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerListModel;


public class DeleteOldDialog extends JDialog implements ActionListener{
	
	private JSpinner dateSelector, hourSelector;
	private JButton ok, cancel;
	/**Flaga określa stan wykonania okna dialogowego*/
	private boolean isExecuted;
	private String  hours[];
	/**
	 * Tworzy Panel umożliwiający wybór daty.
	 * @return gotowy panel.
	 */
	private JPanel createDateSelector ()
	{	 
		
		JPanel DateSelector = new JPanel (new FlowLayout());
		SpinnerListModel hourModel = new SpinnerListModel(hours);
		SpinnerDateModel model = new SpinnerDateModel();
		dateSelector.setModel(model);
        JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(dateSelector, "dd:MM:yy");
        dateSelector.setEditor(dateEditor);
        hourSelector.setModel(hourModel);
        JComponent editor= hourSelector.getEditor();
        JFormattedTextField ftf=((JSpinner.DefaultEditor)editor).getTextField();
        ftf.setColumns(4);
        DateSelector.add(new JLabel("Data:"));
        DateSelector.add(dateSelector);
        DateSelector.add(new JLabel("Godzina:"));
        DateSelector.add(hourSelector);
		 
		return DateSelector;
		
	}
	/**
	 * Tworzy niewidoczne okno dialogowe oraz dodaje wszystkie komponenty.
	 * @param owner Rodzic okna dialogowego.
	 * @param title Tytuł okna dialogowego.
	 */
	DeleteOldDialog (Frame owner, String title)
	{
		super(owner,title,true);
		hours = new String [48];
		for (int h=0,j=0 ; h<24 ; h++,j+=2)
		{
			hours[j]=String.valueOf(h)+":00";
			hours[j+1]=String.valueOf(h)+":30";
		}
		setLayout(new BorderLayout());
		dateSelector = new JSpinner();
		hourSelector = new JSpinner();
		
		
		GridBagConstraints r = new GridBagConstraints();
        r.gridwidth = GridBagConstraints.REMAINDER;
        r.fill = GridBagConstraints.HORIZONTAL;
        r.weightx = 1.0;
		JPanel p = new JPanel(new GridBagLayout());
		p.add(new JLabel ("Usuń wydarzenia starsze niż: "),r);
		p.add(createDateSelector(),r);
		JPanel buttons = new JPanel();
	    buttons.setLayout(new FlowLayout());
	    buttons.add(ok = new JButton("OK"));
	    buttons.add(cancel = new JButton("Anuluj"));
		setLayout(new BorderLayout());
		add(p,BorderLayout.NORTH);
		add(buttons,BorderLayout.SOUTH);
		ok.addActionListener(this);
	    cancel.addActionListener(this);
		setSize(300, 125);
		ok.setMnemonic(KeyEvent.VK_O);
		cancel.setMnemonic(KeyEvent.VK_A);
	}
	/**
	 * Ustawia datę na dzisiejszą 
	 */
	protected void setFields ()
	{
		Calendar c = Calendar.getInstance();
		int h = c.get(Calendar.HOUR_OF_DAY)*2+c.get(Calendar.MINUTE)/30;
		dateSelector.setValue(c.getTime());
		hourSelector.getModel().setValue(hours[h]);
	}
	/**
	 * Pokazuje okno dialogowe, zwraca true jeżeli naciśnięto ok lub false jeżeli anuluj.
	 * @return true jeżeli naciśnięto ok false jeżeli anuluj.
	 */
	public boolean exec()
	{
		setFields();
		setVisible(true);
		return isExecuted;	
	}
	private int indexOf (String s)
	{
		for (int i = 0 ; i<48 ; i++)
		{
			if (hours[i].equals(s)) return i;
		}
		return 0;
	}
	/**
	 * Pobiera datę wybraną przez użytkownika.
	 * @return Data wybrana przez użytkownika.
	 */
	public Calendar getDate ()
	{
		Calendar ctmp=Calendar.getInstance();
		SpinnerDateModel m = (SpinnerDateModel)dateSelector.getModel();
		int t = indexOf((String)hourSelector.getModel().getValue());
		ctmp.setTime(m.getDate());
		ctmp.set(Calendar.HOUR_OF_DAY, t/2);
		ctmp.set(Calendar.MINUTE, t%2*30);
		return ctmp;
	}
	/**
	 * Nasłuchuje przyciski ok i anuluj, ustawia stan flagi isExecuted oraz ukrywa okno.
	 */
	public void actionPerformed(ActionEvent evt) 
	{
		
		if (evt.getSource()==ok)
		{
			isExecuted=true;
			setVisible(false);
		}
		if(evt.getSource()==cancel)
		{
			isExecuted=false;
			setVisible(false);
		}
	}

}

